home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / infodata / callbook.tar / callbook_1.3 / lookup.c < prev    next >
C/C++ Source or Header  |  1991-03-31  |  2KB  |  105 lines

  1. /*
  2.  * Telnet callsign server sources copyright 1989 by Devon Bowen, KA2NRC.
  3.  * You may distribute and modify these files as you please as long as
  4.  * as long as credit to the original creator is given. Please report any
  5.  * bug fixes or modification to bowen@cs.buffalo.edu.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <sys/file.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <syslog.h>
  13.  
  14. #include "callbook.h"
  15.  
  16. extern int output;
  17.  
  18. char *index(), *my_fgets(), *cvtdate();
  19. unsigned short strhash();
  20.  
  21.  
  22. lookup(field, name, printer)
  23. int field;
  24. char *name;
  25. int (*printer)();
  26. {
  27.     FILE *fd;
  28.     int fi, position, i, found_one=0;
  29.     struct stat indata;
  30.     int recl, recf, recn;
  31.     unsigned short hashnum, hash;
  32.     char searchfile[BUFSIZ];
  33.     char datafile[BUFSIZ];
  34.     char buf[256], *search;
  35.  
  36.     hash = strhash(name);
  37.  
  38.     sprintf(searchfile, "%scallsign.%d.search", DB_DIR, field);
  39.     sprintf(datafile, "%scallsign.%d.data", DB_DIR, field);
  40.  
  41.     stat(searchfile, &indata);
  42.  
  43.     recf = 0;
  44.     recl = (indata.st_size / 6) - 1;
  45.     recn = (recl-recf) / 2 + recf;
  46.  
  47.     fi = open(searchfile, O_RDONLY);
  48.  
  49.     (void) lseek(fi, recn*6, L_SET);
  50.     read(fi, &hashnum, sizeof(hashnum));
  51.  
  52.     while (hashnum != hash) {
  53.  
  54.         if (hashnum < hash)
  55.             recf = recn+1;
  56.  
  57.         if (hashnum > hash)
  58.             recl = recn-1;
  59.  
  60.         if (recl < recf)
  61.             return 0;
  62.  
  63.         recn = (recl-recf) / 2 + recf;
  64.  
  65.         (void) lseek(fi, recn*6, L_SET);
  66.         read(fi, &hashnum, sizeof(hashnum));
  67.     }
  68.  
  69.     read(fi, &position, sizeof(position));
  70.  
  71.     close(fi);
  72.  
  73.     fd = fopen(DB_TEXT, "r");
  74.     fi = open(datafile, O_RDONLY);
  75.  
  76.     (void) lseek(fi, position, L_SET);
  77.     read(fi, &position, sizeof(position));
  78.  
  79.     while (position != -1) {
  80.  
  81.         fseek(fd, position, 0);
  82.         search = my_fgets(buf, 255, fd);
  83.  
  84.         for (i=0; i<field; i++)
  85.             search = index(search, '|') + 1;
  86.  
  87.         while (*search == ' ')
  88.             search++;
  89.  
  90.         if (!strncasecmp(search, name, strlen(name)) && filter(buf))
  91.             if (printer(buf, found_one++))
  92.                 break;
  93.  
  94.         read(fi, &position, sizeof(position));
  95.     }
  96.  
  97.     close(fi);
  98.     fclose(fd);
  99.  
  100.     if (found_one)
  101.         return 1;
  102.     else
  103.         return 0;
  104. }
  105.